package com.itheima.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.PmsProductCategory;
import com.itheima.domain.PmsProductCategoryAttributeRelation;
import com.itheima.exception.BusinessException;
import com.itheima.service.db.PmsProductCategoryAttributeRelationService;
import com.itheima.service.db.PmsProductCategoryService;
import com.itheima.util.R;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.PmsProductCategoryVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
public class PmsProductCategoryManager {

    @DubboReference
    PmsProductCategoryService pmsProductCategoryService;
    @DubboReference
    PmsProductCategoryAttributeRelationService ppcarService;

    public R findByPage(Integer pageNum, Integer pageSize, Long parentId) {
        PageBeanVo byPage = pmsProductCategoryService.findByPage(pageNum, pageSize, parentId);
        return R.success(byPage);
    }

    public R findById(Long id) {
        return R.success(pmsProductCategoryService.selectById(id));
    }

    public void navStatus(Long[] ids, Integer navStatus) {
        pmsProductCategoryService.navStatus(ids, navStatus);
    }

    public void showStatus(Long[] ids, Integer showStatus) {
        pmsProductCategoryService.showStatus(ids, showStatus);
    }

    public R create(PmsProductCategory pmsProductCategory) {
        if (pmsProductCategory.getParentId() == 0) {
            pmsProductCategory.setLevel(0);
        } else {
            pmsProductCategory.setLevel(1);
        }
        pmsProductCategory.setProductCount(0);
        //保存后返回, 需要使用到id向分类属性关系表中添加数据
        PmsProductCategory ppcAfterCreate = pmsProductCategoryService.create(pmsProductCategory);
        Long[] productAttributeIdList = pmsProductCategory.getProductAttributeIdList();
        if (productAttributeIdList != null) {
            for (Long id : productAttributeIdList) {
                PmsProductCategoryAttributeRelation ppcar = new PmsProductCategoryAttributeRelation();
                ppcar.setProductAttributeId(id);
                ppcar.setProductCategoryId(ppcAfterCreate.getId());
                ppcarService.save(ppcar);
            }
        }
        return R.success("");
    }

    public R withChildren() {
        return R.success(pmsProductCategoryService.findAll());
    }

    public void update(Long id, PmsProductCategory pmsProductCategory) {
        //处理level字段
        if (pmsProductCategory.getParentId() == 0) {
            pmsProductCategory.setLevel(0);
        } else {
            pmsProductCategory.setLevel(1);
        }
        //修改商品分类表
        pmsProductCategoryService.update(pmsProductCategory);
        //删除商品分类和商品属性关系表之前的数据
        ppcarService.deleteByCategoryId(id);
        //向关系表重新添加更新后的数据
        Long[] attributeIds = pmsProductCategory.getProductAttributeIdList();
        if (attributeIds != null) {
            for (Long attributeId : attributeIds) {
                PmsProductCategoryAttributeRelation ppcar = new PmsProductCategoryAttributeRelation();
                ppcar.setProductCategoryId(id);
                ppcar.setProductAttributeId(attributeId);
                ppcarService.save(ppcar);
            }
        }
    }

    public void delete(Long id) {
        //查询是否还包含下一级
        List<PmsProductCategory> byParentId = pmsProductCategoryService.findByParentId(id);
        if (byParentId == null || byParentId.size() == 0) {
            pmsProductCategoryService.delete(id);
        } else {
            throw new BusinessException("该分类下还包含子分类");
        }
    }
}
